package gestion.camping.vue;

import gestion.camping.controleur.PersonneController;
import gestion.camping.dto.Personne;
import gestion.camping.utils.DateFormatUtils;
import gestion.camping.utils.MessagesUtil;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class ViewPersonneDetail extends JDialog implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -17760458983364266L;
	
	private JTextField textField_Nom;
	private JTextField textField_prenom;
	private JTextField textField_ddn;
	private JTextField textField_adresse;
	
	private JButton btnEnregister;
	private JButton btnAnnuler;
	
	private Personne personne;
	private PersonneController controller;
	private JTextField textField_codePostal;
	private JTextField textField_ville;
	private JLabel lblNewLabel_codePostal;
	private JLabel lblNewLabel_ville;
	private JTextField textField_telephone;
	private JLabel lblNewLabel_telephone;
	private JButton btnRetourListe;

	/**
	 * Create the frame.
	 */
	public ViewPersonneDetail(PersonneController controller) {
	
		this.controller = controller; 
		this.personne = controller.getPersonne();
		
		initCompoment();
		
		setModal(true);
	}
	
	public void initCompoment(){
	
		
		setBounds(0, 25, 700, 500);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0,0, 0, 0};
		gridBagLayout.rowHeights = new int[]{40, 40, 25, 25, 25, 25, 25, 25, 25, 25, 25, 0};
		gridBagLayout.columnWeights = new double[]{0.3, 0.2, 0.2, 0.3};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
		getContentPane().setLayout(gridBagLayout);
		
		btnRetourListe = new JButton(MessagesUtil.formatMessage("personne.ajout.btn.retour"));
		btnRetourListe.addActionListener(this);
		GridBagConstraints gbc_btnRetourListe = new GridBagConstraints();
		gbc_btnRetourListe.anchor = GridBagConstraints.WEST;
		gbc_btnRetourListe.insets = new Insets(0, 0, 5, 0);
		gbc_btnRetourListe.gridx = 3;
		gbc_btnRetourListe.gridy = 0;
		getContentPane().add(btnRetourListe, gbc_btnRetourListe);
		
		JLabel Label_nom = new JLabel(MessagesUtil.formatMessage("personne.ajout.nom"));
		GridBagConstraints gbc_Label_nom = new GridBagConstraints();
		gbc_Label_nom.insets = new Insets(0, 0, 5, 5);
		gbc_Label_nom.anchor = GridBagConstraints.EAST;
		gbc_Label_nom.gridx = 1;
		gbc_Label_nom.gridy = 2;
		getContentPane().add(Label_nom, gbc_Label_nom);
		
		textField_Nom = new JTextField();
		textField_Nom.setText(personne.getNom());
		GridBagConstraints gbc_textField_Nom = new GridBagConstraints();
		gbc_textField_Nom.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_Nom.insets = new Insets(0, 0, 5, 5);
		gbc_textField_Nom.gridx = 2;
		gbc_textField_Nom.gridy = 2;
		getContentPane().add(textField_Nom, gbc_textField_Nom);
		textField_Nom.setColumns(10);
		
		JLabel Label_prenom = new JLabel(MessagesUtil.formatMessage("personne.ajout.prenom"));
		GridBagConstraints gbc_Label_prenom = new GridBagConstraints();
		gbc_Label_prenom.insets = new Insets(0, 0, 5, 5);
		gbc_Label_prenom.anchor = GridBagConstraints.EAST;
		gbc_Label_prenom.gridx = 1;
		gbc_Label_prenom.gridy = 3;
		getContentPane().add(Label_prenom, gbc_Label_prenom);
		
		textField_prenom = new JTextField(personne.getPrenom());
		GridBagConstraints gbc_textField_prenom = new GridBagConstraints();
		gbc_textField_prenom.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_prenom.insets = new Insets(0, 0, 5, 5);
		gbc_textField_prenom.gridx = 2;
		gbc_textField_prenom.gridy = 3;
		getContentPane().add(textField_prenom, gbc_textField_prenom);
		textField_prenom.setColumns(10);
		
		JLabel label = new JLabel(MessagesUtil.formatMessage("personne.ajout.ddn"));
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.anchor = GridBagConstraints.EAST;
		gbc_label.gridx = 1;
		gbc_label.gridy = 4;
		getContentPane().add(label, gbc_label);
		
		textField_ddn = new JTextField(personne.getDateNaissanceString());
		GridBagConstraints gbc_textField_ddn = new GridBagConstraints();
		gbc_textField_ddn.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_ddn.insets = new Insets(0, 0, 5, 5);
		gbc_textField_ddn.gridx = 2;
		gbc_textField_ddn.gridy = 4;
		getContentPane().add(textField_ddn, gbc_textField_ddn);
		textField_ddn.setColumns(10);
		
		JLabel lblNewLabel_adresse = new JLabel(MessagesUtil.formatMessage("personne.ajout.adresse"));
		GridBagConstraints gbc_lblNewLabel_adresse = new GridBagConstraints();
		gbc_lblNewLabel_adresse.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_adresse.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_adresse.gridx = 1;
		gbc_lblNewLabel_adresse.gridy = 5;
		getContentPane().add(lblNewLabel_adresse, gbc_lblNewLabel_adresse);
		
		textField_adresse = new JTextField(personne.getAdresse());
		GridBagConstraints gbc_textField_adresse = new GridBagConstraints();
		gbc_textField_adresse.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_adresse.insets = new Insets(0, 0, 5, 5);
		gbc_textField_adresse.gridx = 2;
		gbc_textField_adresse.gridy = 5;
		getContentPane().add(textField_adresse, gbc_textField_adresse);
		textField_adresse.setColumns(10);
		
		lblNewLabel_codePostal = new JLabel(MessagesUtil.formatMessage("personne.ajout.code.postal"));
		GridBagConstraints gbc_lblNewLabel_codePostal = new GridBagConstraints();
		gbc_lblNewLabel_codePostal.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_codePostal.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_codePostal.gridx = 1;
		gbc_lblNewLabel_codePostal.gridy = 6;
		getContentPane().add(lblNewLabel_codePostal, gbc_lblNewLabel_codePostal);
		
		textField_codePostal = new JTextField(personne.getCodePostal());
		GridBagConstraints gbc_textField_codePostal = new GridBagConstraints();
		gbc_textField_codePostal.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_codePostal.insets = new Insets(0, 0, 5, 5);
		gbc_textField_codePostal.gridx = 2;
		gbc_textField_codePostal.gridy = 6;
		getContentPane().add(textField_codePostal, gbc_textField_codePostal);
		textField_codePostal.setColumns(10);
		
		lblNewLabel_ville = new JLabel(MessagesUtil.formatMessage("personne.ajout.ville"));
		GridBagConstraints gbc_lblNewLabel_ville = new GridBagConstraints();
		gbc_lblNewLabel_ville.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_ville.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_ville.gridx = 1;
		gbc_lblNewLabel_ville.gridy = 7;
		getContentPane().add(lblNewLabel_ville, gbc_lblNewLabel_ville);
		
		textField_ville = new JTextField(personne.getVille());
		GridBagConstraints gbc_textField_ville = new GridBagConstraints();
		gbc_textField_ville.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_ville.insets = new Insets(0, 0, 5, 5);
		gbc_textField_ville.gridx = 2;
		gbc_textField_ville.gridy = 7;
		getContentPane().add(textField_ville, gbc_textField_ville);
		textField_ville.setColumns(10);
		
		lblNewLabel_telephone = new JLabel(MessagesUtil.formatMessage("personne.ajout.telephone"));
		GridBagConstraints gbc_lblNewLabel_telephone = new GridBagConstraints();
		gbc_lblNewLabel_telephone.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_telephone.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_telephone.gridx = 1;
		gbc_lblNewLabel_telephone.gridy = 8;
		getContentPane().add(lblNewLabel_telephone, gbc_lblNewLabel_telephone);
		
		textField_telephone = new JTextField(personne.getNumeroTelephone());
		GridBagConstraints gbc_textField_telephone = new GridBagConstraints();
		gbc_textField_telephone.insets = new Insets(0, 0, 5, 5);
		gbc_textField_telephone.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_telephone.gridx = 2;
		gbc_textField_telephone.gridy = 8;
		getContentPane().add(textField_telephone, gbc_textField_telephone);
		textField_telephone.setColumns(10);
		
		btnEnregister = new JButton(MessagesUtil.formatMessage("personne.ajout.btn.enregistrer"));
		btnEnregister.addActionListener(this);
		GridBagConstraints gbc_btnEnregister = new GridBagConstraints();
		gbc_btnEnregister.anchor = GridBagConstraints.EAST;
		gbc_btnEnregister.insets = new Insets(0, 0, 5, 5);
		gbc_btnEnregister.gridx = 2;
		gbc_btnEnregister.gridy = 10;
		getContentPane().add(btnEnregister, gbc_btnEnregister);
		
		btnAnnuler = new JButton(MessagesUtil.formatMessage("personne.ajout.btn.annuler"));
		btnAnnuler.addActionListener(this);
		GridBagConstraints gbc_btnAnnuler = new GridBagConstraints();
		gbc_btnAnnuler.anchor = GridBagConstraints.WEST;
		gbc_btnAnnuler.insets = new Insets(0, 0, 5, 0);
		gbc_btnAnnuler.gridx = 3;
		gbc_btnAnnuler.gridy = 10;
		getContentPane().add(btnAnnuler, gbc_btnAnnuler);
	}
	
	public void update(){
		textField_Nom.setText(personne.getNom());
		textField_prenom.setText(personne.getPrenom());
		textField_ddn.setText(personne.getDateNaissanceString());
		textField_adresse.setText(personne.getAdresse());
		textField_codePostal.setText(personne.getCodePostal());
		textField_ville.setText(personne.getVille());
		textField_telephone.setText(personne.getNumeroTelephone());
		
		this.repaint();
	}

	public Personne getPersonne() {
		return personne;
	}

	public void setPersonne(Personne personne) {
		this.personne = personne;		
		update();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == this.btnEnregister){
			personne.setNom(this.textField_Nom.getText());
			personne.setPrenom(this.textField_prenom.getText());
			personne.setDateNaissance(DateFormatUtils.parseDate(this.textField_ddn.getText()));
			personne.setAdresse(this.textField_adresse.getText());
			personne.setCodePostal(this.textField_codePostal.getText());
			personne.setVille(this.textField_ville.getText());
			personne.setNumeroTelephone(this.textField_telephone.getText());
		
			controller.enregistrer(personne);
			
		}else if(e.getSource() == this.btnAnnuler){
			update();
		}else if(e.getSource() == this.btnRetourListe){
			
			this.dispose();
			controller.retour();
			
		}
	}
		
}
